<?php

namespace App\Http\Controllers\Api;

use App\Models\AdvContent;
use App\Models\AdvType;
use App\Models\ArticleCategory;
use App\Models\GuideAdv;
use Illuminate\Http\Request;
use App\Models\User;
use App\Http\Controllers\Controller;

class AdvController extends Controller
{
    public $region_id = '';
    public $pagesize = '';

    public function __construct()
    {

        $this -> pagesize  = request() -> input('pagesize') ?? 3;
    }

    // 获取某文章模块类型的广告位
    public function getAdvContent(Request $request) {


        $advTypeId = $request -> input('adv_type_id');

        // 判断是否需要地区帅选
        if(AdvType::adv_region_check($advTypeId)){
            if(!$request -> filled('region_id')){
                return jsonError(4028);
            }
        }

        $advContent = AdvContent::getAdvContent($advTypeId, $this -> pagesize, $request -> input('region_id')?:0);

        return jsonSuccess($advContent);
    }

    /**
     * 首页非地区相关广告位
     * @param Request $request
     * @param INT top_banner_adv_num
     * @param INT category_adv_num
     * @param INT village_online_adv_num
     * @param INT push_goods_adv_num
     * @return false|string
     */
    public function indexAdvContent(Request $request)
    {
        $advContent = [];


        // 分类
        $categoryAdvId = 10;
        $categoryAdvNum = $request->input('category_adv_num') ? $request->input('category_adv_num') : 10;


        // 乡村在线
        $villageOnlineAdvId = 11;
        $villageOnlineAdvNum = $request->input('village_online_adv_num');

        // 农大推荐
        $pushGoodsAdvId = 15;
        $pushGoodsAdvNum = $request->input('push_goods_adv_num');

        $advContent['village_online_adv'] = AdvType::find(11);
        $advContent['village_online_banner'] = AdvContent::getAdvContent($villageOnlineAdvId, $villageOnlineAdvNum);
        $advContent['category_banner'] = AdvContent::getAdvContent($categoryAdvId, $categoryAdvNum);
        $advContent['push_goods_banner'] = AdvContent::getAdvContent($pushGoodsAdvId, $pushGoodsAdvNum);

        return jsonSuccess($advContent);
    }

    /**
     * 首页地区相关广告位
     * @param Request $request
     * @param INT top_banner_adv_num
     * @return false|string
     */
    public function villageIndexAdvContent(Request $request)
    {
        $advContent = [];
        $region_id = $request->input('region_id');


        // 顶部banner
        $topBannerAdvId = 9;
        $topBannerAdvNum = $request->input('top_banner_adv_num') ? $request->input('top_banner_adv_num') : 5;

        // 北汪热点
        $hotAdvId = 12;
        $hotAdvNum = $request->input('village_hot_adv_num') ? $request->input('village_hot_adv_num') : 5;

        // 热点列表
        $hotListAdvId = 13;
        $hotListAdvNum = $request->input('village_hot_list_adv_num') ? $request->input('village_hot_list_adv_num') : 3;

        // 北汪文化
        $wenhuaAdvId = 14;
        $wenhuaAdvNum = $request->input('wenhua_adv_num') ? $request->input('wenhua_adv_num') : 3;

        $advContent['village_hot_adv'] = AdvType::find(12);
        $advContent['top_banner'] = AdvContent::getAdvContent($topBannerAdvId, $topBannerAdvNum, $region_id);
        $advContent['village_hot'] = AdvContent::getAdvContent($hotAdvId, $hotAdvNum, $region_id);
        $advContent['village_hot_list'] = AdvContent::getAdvContent($hotListAdvId, $hotListAdvNum, $region_id);
        $advContent['wenhua'] = AdvContent::getAdvContent($wenhuaAdvId, $wenhuaAdvNum, $region_id);

        return jsonSuccess($advContent);
    }


    /**
     * 首页地区相关广告位
     * @param Request $request
     * @param INT region_id
     * @return false|string
     */
    public function guideAdv(Request $request)
    {
        $region_id = $request -> input('region_id');

        $guideAdv = GuideAdv::where('is_show','1') ->where(function ($query) use($region_id){

            if($region_id){
                $query -> where('region_id',$region_id);
            }else{
                $query -> where('region_id',0);
            }

        }) -> orderBy('id','desc') -> first();

        return jsonSuccess($guideAdv);
    }
}
